<template>
    <div class="app-container">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="字典名称" prop="typeName">
          <el-input
            style="width: 200px"
            v-model="queryParams.typeName"
            placeholder="字典名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="字典编码" prop="typeCode">
          <el-input
            style="width: 200px"
            v-model="queryParams.typeCode"
            placeholder="字典编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="plus"
            @click="handleAdd"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="delete"
            :disabled="multiple"
            @click="handleDelete"
            >删除</el-button
          >
        </el-col>
      </el-row>
      <br />
      <el-table
        v-loading="loading"
        :data="typeList"
        stripe
        table-layout="fixed"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" />
        <!-- <el-table-column label="字典主键" prop="typeId" /> -->
        <el-table-column label="字典名称" prop="typeName" />
        <el-table-column label="字典编码" prop="typeCode" />
        <el-table-column label="备注" prop="remark" />
  
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button
              size="small"
              type="primary"
              text
              icon="DocumentAdd"
              @click="handleDict(scope.row)"
              >字典项</el-button>
            <el-button
                size="small"
                type="primary"
                text
                icon="edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button>
            <el-button
                size="small"
                type="primary"
                text
                icon="delete"
                @click="handleDelete(scope.row)"
                >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <el-pagination
        size="small" 
        layout="prev, pager, next, jumper, ->, total"
        :total="total"
        :page-size="queryParams.pageSize"
        :current-page="queryParams.pageNum"
        :page-count="pages"
        @current-change="getList"
      />
  
        <!-- 添加或修改问卷对话框 -->
        <el-dialog :title="title" v-model="open" append-to-body>
            <el-form ref="dialogForm" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="字典名称" prop="typeName">
                <el-input v-model="form.typeName" placeholder="请输入字典名称" />
            </el-form-item>
            <el-form-item label="字典编码" prop="typeCode">
                <el-input v-model="form.typeCode" placeholder="请输入字典编码" />
            </el-form-item>
            <el-form-item label="说明" prop="remark">
                <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                />
            </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="handleSubmit">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>

        <!-- 设置字典项的对话框 -->
        <el-dialog :title="dtitle" v-model="dopen" append-to-body>
            <el-button type="primary" plain icon="plus" @click="addDict">新增字典项</el-button>
            <el-table v-loading="loading" :data="dictList" stripe table-layout="fixed">
                <el-table-column label="字典名称" prop="dictName" />
                <el-table-column label="字典编码" prop="dictValue" />
                <el-table-column label="备注" prop="remark" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <el-button size="small" type="primary" text icon="edit" @click="addDict(scope.row)" >修改</el-button>
                        <el-button size="small" type="primary" text icon="delete" @click="delDict(scope.row)" >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>

        <!-- 添加或修改字典对话框 -->
        <el-dialog :title="ntitle" v-model="nopen" append-to-body>
            <el-form ref="nform" :model="nform" :rules="nrules" label-width="80px">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="字典名称" prop="dictName">
                            <el-input v-model="nform.dictName" placeholder="请输入字典名称" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="字典编码" prop="dictValue">
                            <el-input v-model="nform.dictValue" placeholder="请输入字典编码" />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item label="默认值" prop="isDefault">
                    <el-radio-group v-model="nform.isDefault">
                        <el-radio label="Y">是</el-radio>
                        <el-radio label="N">否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="标签样式" prop="cssClass">
                  <el-radio-group v-model="nform.cssClass" aria-label="label position">
                    <el-radio-button v-for="tag in tags" :key="tag" :value="tag">{{ tag }}</el-radio-button>
                  </el-radio-group>
                  <el-tag style="margin-left: 10px;" :type="nform.cssClass">显示这样</el-tag>
                </el-form-item>
                <el-form-item label="说明" prop="remark">
                    <el-input v-model="nform.remark" type="textarea" placeholder="请输入内容" />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="dictSublimt">确 定</el-button>
                <el-button @click="dictCancel">取 消</el-button>
            </div>
        </el-dialog>

    </div>
  </template>
  
  <script>
  import {
    listType,
    getType,
    saveOrUpdateType,
    removeType,
    listDict,
    getDict,
    saveOrUpdateDict,
    removeDict
  } from "@/api/dict";
  import { ElMessageBox } from 'element-plus'
  
  export default {
    name: "Dict",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 总页数
        pages: 0,
        // 问卷表格数据
        typeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          typeName: null,
          typeCode: null,
        },
        // 表单参数
        form: {
          typeName: '',
          typeCode: '',
          remark: '',
        },
        // 表单校验
        rules: {
            typeName: [
                { required: true, message: "字典名称不能为空", trigger: "blur" },
            ],
            typeCode: [
                { required: true, message: "字典编码不能为空", trigger: "blur" },
            ],
        },

        // 字典项表格数据
        dictList: [],
        dtitle: "",
        dopen: false,

        ntitle: "",
        nopen: false,
        nform: {
            dictId: '',
            dictName: '',
            dictValue: '',
            isDefault: 'N',
            remark: '',
            typeId: '',
            typeCode: '',
        },
        nrules: {
            dictName: [
                { required: true, message: "字典名称不能为空", trigger: "blur" },
            ],
            dictValue: [
                { required: true, message: "字典编码不能为空", trigger: "blur" },
            ],
        },
        tags: ["primary", "success", "info", "warning", "danger"],
      };
    },
    async created() {
      this.getList();
    },
    methods: {
      /** 查询问卷列表 */
      getList() {
        this.loading = true;
        listType(this.queryParams).then((response) => {
          this.typeList = response.records;
          this.total = response.total;
          this.pages = response.pages;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          typeId: null,
          dictName: null,
          dictCode: null,
          remark: null,
        };
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$refs.queryForm.resetFields();
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.surveyId);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加字典";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const typeId = row.typeId || this.ids;
        getType(typeId).then((response) => {
          this.form = response;
          this.open = true;
          this.title = "修改字典";
        });
      },
      /** 提交按钮操作 */
      handleSubmit(){
        this.$refs.dialogForm.validate((valid) => {
            if (valid) {
                saveOrUpdateType(this.form).then(() => {
                    this.$message({message: '操作成功',type:'success'});
                    this.open = false;
                    this.getList();
                });
            }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const typeIds = row.typeId || this.ids;
        ElMessageBox.confirm(
          '是否确认删除问卷编号为" ' + typeIds + ' "的字典吗？',
          '警告',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
        ).then(async () => {
            await removeType(typeIds);
            this.getList();
            this.$message({message: '删除成功',type:'success'});
        }).catch(() => {});
      },

      // 字典项列表
      getDictList(typeId){
        listDict({"typeId": typeId}).then((response) => {
          this.dictList = response;
        });
      },
      handleDict(row) {
        this.getDictList(row.typeId)
        this.dopen = true;
        this.dtitle = "字典项";

        this.nform.typeId = row.typeId;
        this.nform.typeCode = row.typeCode;
      },
      // 新增字典项
      addDict(row) {
        const dictId = row.dictId;
        if (dictId) {
            getDict(dictId).then((response) => {
                this.nform = response;
                this.nopen = true;
                this.ntitle = "修改字典项";
            });
        } else {
            this.nopen = true;
            this.ntitle = "新增字典项";
        }
      },
      delDict(row) {
        const dictId = row.dictId
        ElMessageBox.confirm('是否确认删除字典项编号为"'+ dictId +'"的字典项吗？','警告',{
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }
        ).then(async () => {
            await removeDict(dictId);
            this.getDictList(this.nform.typeId);
            this.$message({message: '删除成功',type:'success'});
        }).catch(() => {});
      },
      dictSublimt(){
        this.$refs.nform.validate((valid) => {
            if (valid) {
                saveOrUpdateDict(this.nform).then(() => {
                    this.$message({message: '操作成功',type:'success'});
                    this.nopen = false;
                    this.getDictList(this.nform.typeId);
                });
            }
        })
      },
      dictCancel(){
        this.nopen = false;
      },
      tagChange(val){
        console.log(val);

      }
    },
  };
  </script>
  
  <style scoped>
  .el-button.el-button--primary.el-button--small.is-text{
    padding: 0 4px;
    margin: 0;
  }
  .showcase {
    font-size: 12px;
    vertical-align: middle;
    margin-left: 10px;
  }
  .showcase .el-dropdown-link {
    cursor: pointer;
    color: var(--el-color-primary);
    display: flex;
    align-items: center;
  }
  </style>
  